import { renderAsciiFromUrl, streamText } from '../utils/tools';

export default class Printer {
    constructor(options = {
        asciiImage: false,
        asciiImageMaxWidth: 100,
    }) {
        this.history = [];
        this.loading = false;
        this.loadingCharacter = '';
        this.streaming = false;
        this.asciiConfig = {
            enabled: options?.asciiImage || false,
            maxWidth: options?.asciiImageMaxWidth || 100,
        };
    }

    async print(text, type = 'output', dataType = 'text') {
        if (dataType === 'ascii-image') {
            try {
                const str = await renderAsciiFromUrl({ url: text, maxWidth: this.asciiConfig.maxWidth });
                this.history.push({ type: type, text: str, dataType: 'ascii-image' });
            } catch (e) {
                this.history.push({ type: type, text: '[ 图片加载失败 ]' });
            }
        } else if (dataType === 'image') {
            this.history.push({ type: type, text: text, dataType: 'image' });
        } else {
            this.history.push({ type: type, text: text });
        }
    }
    stream(text) {
        if (typeof text === 'boolean' && !text) {
            this.streaming = false;
            return;
        }
        if (this.streaming) {
            this.history[this.history.length - 1].text += text;
        } else {
            this.history.push({ type: 'output', text: text });
            this.streaming = true;
        }
    }
    clear() {
        this.history.length = 0;
        this.loading = false;
        this.loadingCharacter = '';
    }
    startLoading(text = '正在处理') {
        this.loading = true;
        if (loadingInterval) return;

        loadingInterval = setInterval(() => {
            const frame = frames[i = ++i % frames.length];
            this.loadingCharacter = frame;
        }, 100);
    }
    stopLoading() {
        clearInterval(loadingInterval);
        loadingInterval = null;
        this.loading = false;
    }
    async autoPrint({ status, data, message }, isStream, id, source) {
        if (status === 'START') {
            this.startLoading();
        }
        if (status === 'ERROR') {
            if (message && typeof message === 'string') {
                this.print('😭 ' + message);
                this.stopLoading();
                this.stream(false);
            }
        } else if (isStream && data && typeof data === 'string') {
            this.stream(data);
        } else if (status === 'MEME_START') {
            this.stream(false);
        } else if (status === 'MEME_END') {
            if (this.asciiConfig.enabled) {
                await this.print(data, 'output', 'ascii-image');
                terminal.stream(false);
            } else {
                terminal.print(data, 'output', 'image');
            }
        }
        if (status === '[END]') {
            this.stopLoading();
            this.stream(false);
        }
    }
}

const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
let i = 0;
let loadingInterval = null;